package com.fankui.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankui.commonutils.R;
import com.fankui.serviceedu.entity.EduCourse;
import com.fankui.serviceedu.entity.enums.CourseStatusEnum;
import com.fankui.serviceedu.entity.vo.CoursePublishVo;
import com.fankui.serviceedu.entity.vo.CourseQuery;
import com.fankui.serviceedu.entity.vo.CourseVo;
import com.fankui.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 范奎
 * @since 2023-11-25
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "添加课程")
    @PostMapping("addCourse")
    public R addCourse(@ApiParam(name = "courseVo", value = "添加课程VO对象", required = true) @RequestBody CourseVo courseVo) {
        String courseId = courseService.saveCourse(courseVo);
        return R.ok().data("courseId", courseId);
    }

    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("getCourse/{courseId}")
    public R getCourse(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        CourseVo courseVo = courseService.getCourseById(courseId);
        return R.ok().data("courseVo", courseVo);
    }

    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("updateCourse")
    public R updateCourse(@ApiParam(name = "courseVo", value = "修改课程VO对象", required = true) @RequestBody CourseVo courseVo) {
        boolean flag = courseService.updateCourse(courseVo);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping("getCoursePublish/{courseId}")
    public R getCoursePublish(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        CoursePublishVo coursePublish = courseService.getCoursePublishById(courseId);
        return R.ok().data("coursePublish", coursePublish);
    }

    @ApiOperation(value = "课程发布")
    @PostMapping("coursePublish/{courseId}")
    public R coursePublish(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus(CourseStatusEnum.NORMAL.getCode());
        boolean flag = courseService.updateById(eduCourse);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "条件查询分页课程数据")
    @PostMapping("pageCourseCondition/{current}/{size}")
    public R pageCourseCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                 @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable long size,
                                 @ApiParam(name = "courseQuery", value = "条件查询对象") @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> coursePage = new Page<>(current, size);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getTeacherId())) {
            wrapper.eq("teacher_id", courseQuery.getTeacherId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            wrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            wrapper.like("title", courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())) {
            wrapper.eq("status", courseQuery.getStatus());
        }
        if (!StringUtils.isEmpty(courseQuery.getBegin())) {
            wrapper.ge("gmt_create", courseQuery.getBegin());
        }
        if (!StringUtils.isEmpty(courseQuery.getEnd())) {
            wrapper.le("gmt_modified", courseQuery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        courseService.page(coursePage, wrapper);
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("removeCourse/{courseId}")
    public R removeCourse(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        boolean flag = courseService.removeCourseById(courseId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

